﻿using studentPortal.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace studentPortal.Controllers
{
    public class UserController : Controller
    {
        IAccount ac;


        public UserController(IAccount ac1)
        {
            ac = ac1;
        }
        //
        // GET: /User/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ProfilePage()
        {
            if (Session["username"] != null)
            {
                String s = Session["username"].ToString();
                user q = ac.getUser(s);

                educationAndexperience ed = ac.getEducation(s);
                if(ed == null)
                {

                }
                ViewBag.x = ed;
                return View(q);    
            }
            else
            {
                return Redirect("../Account/signin");
            }
            
        }


        public JsonResult UpdateProfile(String f, String n)
        {
            try
            {
                //var q = ac.getUser(u);
                String username = Session["username"].ToString();
                Database1Entities1 cx = new Database1Entities1();
                var q = cx.users.First(x => x.username.Equals(username));
                if(n.Equals("1"))
                    q.fname = f;
                else if (n.Equals("2"))
                    q.lname = f;
                else if (n.Equals("3"))
                    q.dob = f;
                else if (n.Equals("4"))
                    q.gender = f;
                else if (n.Equals("5"))
                    q.address = f;
                else if (n.Equals("10"))
                    q.email = f;
                else if (n.Equals("7"))
                    q.phone = f;
                else if (n.Equals("9"))
                    q.pwd = f;
                else if (n.Equals("11"))
                    q.type = f;
                else if (n.Equals("12"))
                    q.question = f;
                else if (n.Equals("13"))
                    q.answer = f;
                cx.SaveChanges();

                if (q != null)
                    return this.Json(true, JsonRequestBehavior.AllowGet);
                else
                    return this.Json(false, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return this.Json(false, JsonRequestBehavior.AllowGet);
            }
        }


        public JsonResult UpdateEducation(String f, String n)
        {
            try
            {
                //var q = ac.getUser(u);
                String username = Session["username"].ToString();
                Database1Entities1 cx = new Database1Entities1();
                var q = cx.educationAndexperiences.First(x => x.username.Equals(username));
                
                if(n.Equals("14"))
                    q.degree = f;
                else if (n.Equals("15"))
                    q.experties = f;
                else if (n.Equals("16"))
                    q.qualification = f;
                else if (n.Equals("17"))
                    q.workPlace = f;
                else if (n.Equals("18"))
                    q.experience = f;

                cx.SaveChanges();

                if (q != null)
                    return this.Json(true, JsonRequestBehavior.AllowGet);
                else
                    return this.Json(false, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return this.Json(false, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult post(String pid)
        {
            int postId = int.Parse(pid);
            Database1Entities1 cx = new Database1Entities1();
            post p = cx.posts.First(x => x.postId == postId);
            return View(p);
        }

        public ActionResult createPost(post p)
        {
            Database1Entities1 cx = new Database1Entities1();

            String owner = Session["username"].ToString();
            p.owner = owner;
            p.time = DateTime.Now.ToString();

            cx.posts.Add(p);
            cx.SaveChanges();
            return Redirect("../Group/Group?id=" + p.groupId);
        }

        public JsonResult likes(String pid,String op)
        {
            try
            {
                Database1Entities1 cx= new Database1Entities1();
                int pId = int.Parse(pid);
                String username = Session["username"].ToString();
                var q = cx.posts.First(x=>x.postId==pId);
                if (q != null)
                {
                    if (op.Equals("pe"))
                    {
                        q.like += 1;
                        like l = new like();
                        l.cId = null;
                        l.pId = pId;
                        l.username = username;
                        l.status = 1;
                        cx.likes.Add(l);
                    }
                    else
                    {
                        q.like -= 1;
                        like v = cx.likes.First(x=>x.pId == pId && x.username.Equals(username));
                        var rm = cx.likes.Attach(v);
                        cx.likes.Remove(rm);
                    }
                    cx.SaveChanges();
                    return this.Json(q.like, JsonRequestBehavior.AllowGet);
                }

                else
                    return this.Json(-1, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                try
                {
                    String user1 = Session["username"].ToString();
                }
                catch
                {
                        return this.Json(-5, JsonRequestBehavior.AllowGet);
                }
                    
                return this.Json(-1, JsonRequestBehavior.AllowGet);
            }

        }

        public JsonResult unlikes(String pid, String op)
        {
            try
            {
                Database1Entities1 cx = new Database1Entities1();
                int pId = int.Parse(pid);
                String username = Session["username"].ToString();
                var q = cx.posts.First(x => x.postId == pId);
                if (q != null)
                {
                    if (op.Equals("pe"))
                    {
                        q.dislike += 1;
                        like l = new like();
                        l.cId = null;
                        l.pId = pId;
                        l.username = username;
                        l.status = 0;
                        cx.likes.Add(l);
                    }
                    else
                    {
                        q.dislike -= 1;
                        like v = cx.likes.First(x => x.pId == pId && x.username.Equals(username));
                        var rm = cx.likes.Attach(v);
                        cx.likes.Remove(rm);
                    }
                    cx.SaveChanges();
                    return this.Json(q.dislike, JsonRequestBehavior.AllowGet);
                }

                else
                    return this.Json(-1, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                try
                {
                    String user1 = Session["username"].ToString();
                }
                catch
                {
                    return this.Json(-5, JsonRequestBehavior.AllowGet);
                }
                return this.Json(-1, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult userLike(String id, String status)
        {
            try
            {
                Database1Entities1 cx = new Database1Entities1();
                int Id = int.Parse(id);
                String username = Session["username"].ToString();
                like q = cx.likes.First(x => x.pId == Id && x.username.Equals(username));
                if (q != null)
                {
                    return this.Json(q.status, JsonRequestBehavior.AllowGet);
                }

                else
                    return this.Json(9, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return this.Json(9, JsonRequestBehavior.AllowGet);
            }
        }

    }
}
